package com.danger.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.danger.Activity.R;
import com.danger.Model.Student;

import java.util.List;

/**
 * Created by 俊 on 2016/5/23.
 */
public class StuAdapter extends BaseAdapter {

    private Context context;
    private List<Student> list;

    public StuAdapter(Context context, List<Student> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_stu, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final Student student = list.get(position);
        holder.textView.setText(student.getName());
        holder.delete_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                student.delete();
                list.remove(position);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    public static class ViewHolder {

        public TextView textView;
        public Button delete_button;

        public ViewHolder(View view) {
            textView = (TextView) view.findViewById(R.id.textView);
            delete_button = (Button) view.findViewById(R.id.delete_button);
        }
    }
}
